//
//  MoreRadioViewController.swift
//  FM
//
//  Created by x on 2017/11/1.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class MoreRadioViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        navigationController?.navigationBar.isHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        navigationController?.navigationBar.isHidden = false
    }

    /// 初始化
    private func setupUI() -> Void {
        view.backgroundColor = UIColor.white
        automaticallyAdjustsScrollViewInsets = false
        view.addSubview(radioTableView)
        
        let btu = UIButton.buttonWith(image: "more222", hightImage: "", tag: 1, target: self, action: #selector(leftBtuClick))
        btu.translatesAutoresizingMaskIntoConstraints = true
        btu.frame = CGRect(x: 10, y: 20, width: 30, height: 30)
        view.addSubview(btu)
        
        //加载数据
        NetWorTools.shareNetWorkTools().loadMoreRadio { (data, error) in
            if error == nil {
                self.radioList = data!
                self.radioTableView.reloadData()
            }else {
                print("loadMoreRadio=======",error ?? 0)
            }
        }
    }
    
    func leftBtuClick() -> Void {
        navigationController?.popViewController(animated: true)
    }
    
    
    /// 跳转到电台详情界面
    ///
    /// - Parameter radio: <#radio description#>
    func pushController(radio:Radio) -> Void {
        //1.跳转界面加载详情
        let radioDetailsController = RadioDetailsViewController()
        radioDetailsController.currentRadio = radio
        navigationController?.pushViewController(radioDetailsController, animated: true)
    }
    
    //MARK: -- 懒加载
    
    private lazy var radioTableView:UITableView = {
        let tableView = UITableView()
        tableView.frame = self.view.bounds
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        
        tableView.register(HomeRadioTableViewCell.self, forCellReuseIdentifier: moreRadioCellIdentifier)
        tableView.register(HotRadioTableViewCell.self, forCellReuseIdentifier: hotRadioCellIdentifier)
        tableView.register(MoreRadioHeadView.self, forHeaderFooterViewReuseIdentifier: moreHeadViewIdentifier)
        
        return tableView
    }()
    
    fileprivate lazy var radioList:[[Radio]] = [[Radio]]()
    
    fileprivate lazy var recommedView:RecommendView = {
        let tt = RecommendView(frame: CGRect.init(x: 0, y:0, width: self.view.bounds.width, height: 150))
        tt.delegate = self
        
        return tt
    }()
    
    fileprivate lazy var cachesHeight:[String:CGFloat] = [String:CGFloat]()
}


private let moreRadioCellIdentifier = "moreRadioCellIdentifier"

private let hotRadioCellIdentifier = "hotRadioCellIdentifier"

private let moreHeadViewIdentifier = "moreHeadViewIdentifier"
extension MoreRadioViewController:UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return radioList.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 0
        case 1: return 1
        default:
            return radioList[section].count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: moreRadioCellIdentifier) as! HomeRadioTableViewCell
            cell.delegate = self
            cell.radios = radioList[indexPath.section]
            
            return cell
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: hotRadioCellIdentifier) as! HotRadioTableViewCell
            let models = radioList[indexPath.section]
            cell.radio = models[indexPath.row]
            
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.section == 2 {
            pushController(radio: radioList[indexPath.section][indexPath.row])
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        switch section {
        case 0:
            //传递一个url数组
            let radios = (radioList[section])
            var urls:[URL] = [URL]()
            for model in radios {
                if model.coverImageURL != nil {
                    urls.append(model.coverImageURL!)
                }
            }
            recommedView.models = urls

            return recommedView
        default:
            let headview = tableView.dequeueReusableHeaderFooterView(withIdentifier: moreHeadViewIdentifier) as! MoreRadioHeadView
            headview.headIndex = section
            headview.delegate = self
            
            return headview
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        switch section {
        case 0:
            return recommedView.frame.height
        default:
            return 30
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch indexPath.section {
        case 1:
            return (view.bounds.width-40)/4 + 20
            
        case 2:
            let radio = radioList[indexPath.section][indexPath.row]
            let height = cachesHeight[radio.id!]
            if height != nil{
                return height!
            }
            
            let cell = tableView.dequeueReusableCell(withIdentifier: hotRadioCellIdentifier) as! HotRadioTableViewCell
            
            let rowHeight = cell.rowHeight(model: radio)
            
            cachesHeight[radio.id!] = rowHeight
            
            return rowHeight

        default:
            return 0
        }
    }
}

extension MoreRadioViewController:RecommendViewDelegate,HomeTableHeadViewDelegate,HomeRadioTableViewCellDelegate {
    
    func btuWillClick(index: Int) {
        pushController(radio: (radioList.first?[index])!)
    }
    
    func moreBtuDidClick(currentIndex: Int) {
        if currentIndex == 1 {
            let moreAnchorVC = MoreAnchorViewController()
            navigationController?.pushViewController(moreAnchorVC, animated: true)
        }
    }
    
    func radioDetailsButtonWillClick(radio: Radio) {
        pushController(radio: radio)
    }
}






